import dayjs from "dayjs";

/**
 * 将时间格式化为 YYYY-MM-DD HH:mm:ss 格式
 * @param time
 */
export const formatDateTime = (time: dayjs.ConfigType) => dayjs(time).format("YYYY-MM-DD HH:mm:ss");

/**
 * 将时间格式化为 YYYY-MM-DD HH:mm 格式
 * @param time
 */
export const formatDateTime1 = (time: dayjs.ConfigType) => dayjs(time).format("YYYY-MM-DD HH:mm");

export const getStartTime = (v: dayjs.Dayjs | undefined) => v && v.set("hour", 0).set("minute", 0).set("second", 0).set("millisecond", 0).valueOf();
export const getEndTime = (v: dayjs.Dayjs | undefined) =>
  v && v.set("hour", 23).set("minute", 59).set("second", 59).set("millisecond", 999).valueOf();

export function formatSeconds(seconds: number, showMs = false, msLen = 3) {
  // 将秒数格式化为 HH:MM:SS格式
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const remainingSeconds = Math.floor(seconds % 60);

  let s = `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  if (showMs) {
    const ms = Math.round((seconds % 1) * 1000);
    s += `.${ms.toString().padEnd(msLen, "0")}`;
  }
  return s;
}

const dateNames = ["年", "个月", "星期", "天", "小时", "分钟", "秒"];
const dateSeconds = [31536000, 2592000, 604800, 86400, 3600, 60, 1];

/**
 * 将毫秒数格式化为(天/小时/分钟/秒)的格式
 * @param millseconds
 */
export function formatMillseconds(millseconds: number) {
  let seconds = Math.floor(millseconds / 1000);
  return formatSeconds1(seconds);
}

/**
 * 将秒数格式化为(天/小时/分钟/秒)的格式
 * @param seconds 秒数
 */
export function formatSeconds1(seconds: number) {
  let retStr = "";
  for (let i = 0; i < dateSeconds.length - 1; i++) {
    const v = seconds / dateSeconds[i];
    if (v > 1) {
      retStr += Math.floor(v) + dateNames[i];
    }
    seconds = seconds % dateSeconds[i];
  }
  if (seconds > 0) {
    retStr += Math.floor(seconds).toString().padStart(2, "0") + dateNames[dateNames.length - 1];
  }

  return retStr;
}

export type FormatFriendlyType = "default" | "-" | "/" | "." | "年月日" | "月日" | "年";

/**
 * 将时间格式化为"刚刚,xx分钟前,xx小时前,xx天前,xx月前,xx年前"
 * @param date 时间
 * @param type 格式化类型
 * @param zeroFillFlag type!="default"时，是否补零
 */
export function formatFriendly(date?: dayjs.ConfigType, type: FormatFriendlyType | string = "default", zeroFillFlag = true) {
  if (!date) {
    return "";
  }

  // 当前时间
  const now = dayjs();
  // 过去的时间
  const past = dayjs(date);

  // 时间差（毫秒）
  const timeDiff = now.valueOf() - past.valueOf();
  // 时间差转换为秒、分钟、小时和天
  const seconds = Math.floor(timeDiff / 1000);
  const minutes = Math.floor(timeDiff / 60000);
  const hours = Math.floor(timeDiff / 3600000);
  const days = Math.floor(timeDiff / 86400000);
  // 时间差换算为年和月
  const years = Math.floor(days / 365);
  const months = Math.floor(days / 30);

  if (type === "default") {
    // 根据时间差返回相对时间描述
    if (timeDiff < 10000) return "刚刚";
    if (timeDiff < 60000) return `${seconds}秒前`;
    if (timeDiff < 3600000) return `${minutes}分钟前`;
    if (timeDiff < 86400000) return `${hours}小时前`;

    // 如果时间差表示的时间超过1年、1月或1天，返回对应的描述
    if (years >= 1) return `${years}年前`;
    if (months >= 1 && days >= 30) return `${months}个月前`;
    if (days >= 1) return `${days}天前`;
  }

  // 获取过去的年、月、日
  const year = past.year();
  const month = past.month() + 1;
  const day = past.date();

  // 补零函数，如果 zeroFillFlag 为 true 且数值小于 10，则补零
  const zeroFill = (n: number) => (zeroFillFlag && n < 10 ? "0" : "") + n;

  // 根据 type 返回指定格式的日期字符串
  switch (type) {
    case "-":
    case "/":
    case ".":
      return `${year}${type}${zeroFill(month)}${type}${zeroFill(day)}`;
    case "年月日":
      return `${year}年${zeroFill(month)}月${zeroFill(day)}日`;
    case "月日":
      return `${zeroFill(month)}月${zeroFill(day)}日`;
    case "年":
      return `${year}年`;
    default:
      return `${zeroFill(month)}/${zeroFill(day)}/${year}`;
  }
}
